package Sort;

import java.util.Arrays;

//冒泡排序
public class BubbleSort {
    public static void  bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(!flg){
                return;
            }
        }
    }

    public static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void  TestSort(int[] array){
        int[] tmp = Arrays.copyOf(array,array.length);
        long time1 = System.currentTimeMillis();
        bubbleSort(tmp);
        long time2 = System.currentTimeMillis();
        System.out.println("排序用时："+(time2-time1));

    }

    //创建最坏情况的排序
    public static void createArray(int[] array){
        for (int i = 0; i < array.length ; i++) {
            array[i] = array.length-i;
        }
    }

    public static void main(String[] args) {
        int[] array = new int[10_0000];
        //完全逆序
        createArray(array);
        TestSort(array);
    }
}
